using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BulletObj : MonoBehaviour
{
    public TankBase own;

    public float speed = 100;

    public GameObject bulletEffect;

    private AudioSource audioSource;

    // Start is called before the first frame update
    void Start()
    {
    }

    public void SetOwn(TankBase tankBase)
    {
        own = tankBase;
    }

    // Update is called once per frame
    void Update()
    {
        this.transform.Translate(Vector3.forward * speed * Time.deltaTime);
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Wall") ||
            (own.tag.Equals("Player") && other.CompareTag("Monster")) ||
            (own.tag.Equals("Monster") && other.CompareTag("Player")))
        {
            //如果触发器碰到的是玩家坦克，则需要扣血
            TankBase tankBase = other.GetComponent<TankBase>();
            if (tankBase != null)
            {
                tankBase.Hurt(own);
            }

            //子弹特效
            GameObject bulletEffectTemp = Instantiate(bulletEffect, this.transform.position, this.transform.rotation);
            if (audioSource == null)
            {
                audioSource = bulletEffectTemp.GetComponent<AudioSource>();
            }

            audioSource.volume = GameDataManager.Instance.musicData.soundValue;
            audioSource.mute = !GameDataManager.Instance.musicData.isOpenSound;
            Destroy(this.gameObject);
        }
    }
}